import {
  queryOverviewInfo,
  queryVillageleak,
  querySellWater,
  queryDivisionCopyrate,
  queryScadaTpl,
  queryMaxAccount,
  queryStationAlarm,
  queryRealtimeWater3,
  queryRealtimeMeter3,
} from '../services/index';

export default {
    namespace: 'watermeter',
  
    state: {
      OverviewInfoResource: {},
      MaxAccountResource: {},
      MaxAccountLoading: true,
      StationAlarmLoading: true,
      AnalysisResource1: [
        { title: '正常', val: 79 },
        { title: '异常', val: 21 },
      ],
      AnalysisResource22: [
        { time: '2019-01', sell: 0, subtract: 0, radio1: 0, radio: 0 },
        { time: '2019-02', sell: 0, subtract: 0, radio1: 0, radio: 0 },
        { time: '2019-03', sell: 0, subtract: 0, radio1: 0, radio: 0 },
        { time: '2019-04', sell: 0, subtract: 0, radio1: 0, radio: 0 },
        { time: '2019-05', sell: 0, subtract: 0, radio1: 0, radio: 0 },
        { time: '2019-06', sell: 0, subtract: 0, radio1: 0, radio: 0 },
        { time: '2019-07', sell: 0, subtract: 0, radio1: 0, radio: 0 },
        { time: '2019-08', sell: 0, subtract: 0, radio1: 0, radio: 0 },
        { time: '2019-09', sell: 0, subtract: 0, radio1: 0, radio: 0 },
        { time: '2019-10', sell: 0, subtract: 0, radio1: 0, radio: 0 },
        { time: '2019-11', sell: 0, subtract: 0, radio1: 0, radio: 0 },
        { time: '2019-12', sell: 0, subtract: 0, radio1: 0, radio: 0 },
      ]
    },
  
    effects: {
      *queryOverviewInfo({ payload }, { call, put }) {
        const data = yield call(queryOverviewInfo, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              OverviewInfoResource: other.Response,
            },
          })
        } else {
          throw data
        }
      },
      *queryVillageleak({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(queryVillageleak, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`VillageleakResource${type}`]: other.Response.Report.Data,
            },
          })
        } else {
          throw data
        }
      },
      // 售水量
      *querySellWater({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(querySellWater, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`SellWaterResource${type}`]: other.data,
            },
          })
        } else {
          throw data
        }
      },
      // 抄件率
      *queryDivisionCopyrate({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(queryDivisionCopyrate, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`DivisionCopyrateResource${type}`]: other.Response[0],
            },
          })
        } else {
          throw data
        }
      },
      *queryScadaTpl({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(queryScadaTpl, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`ScadaTplResource${type}`]: other.Response,
            },
          })
        } else {
          throw data
        }
      },
      *queryMaxAccount({ payload: params }, { call, put }) {
        const { loading, ...payload } = params;
        const data = yield call(queryMaxAccount, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              MaxAccountResource: other.data,
              MaxAccountLoading: false
            },
          })
        } else {
          throw data
        }
      },
      *queryStationAlarm({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(queryStationAlarm, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`StationAlarmResource${type}`]: other.Response,
              [`StationAlarmLoading${type}`]: false
            },
          })
        } else {
          throw data
        }
      },
      *queryRealtimeWater3({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(queryRealtimeWater3, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`RealtimeWater3Resource${type}`]: other.Response
            },
          })
        } else {
          throw data
        }
      },
      *queryRealtimeMeter3({ payload: params }, { call, put }) {
        const { type, ...payload } = params;
        const data = yield call(queryRealtimeMeter3, payload)
        const { success, message, status, ...other } = data
        if (success) {
          yield put({
            type: 'setState',
            payload: {
              [`RealtimeMeter3Resource${type}`]: other.Response
            },
          })
        } else {
          throw data
        }
      },
    },
  
    reducers: {
      setState(state, action) {
        return {
            ...state,
            ...action.payload
        };
      }
    },
  }
  